<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Uindex extends Admin_Controller {
	public $resources = "";
	public $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	public $model = "";
	public $view = "";
	function __construct() {
        parent::__construct();
        $this->load->model('Admin_m','admin');
        $this->load->model('Permission_m','permission');
		$this->resources = new stdClass;
		//$this->load->model("mresourcegroups");
    }
	 public function home($page = 0) {
        $this->heading = 'Bảng điều khiển';
        if ($this->admin->checkLogin()) {
        	$this->load->model('post_m');
        	$this->load->model('log_post_audit_m');
        	$this->load->model('Log_admin_activities_m');
        	$this->data['log_audits'] = $this->log_post_audit_m->select('log_post_audit.*, posts.post_title')->join('posts','posts.post_id = log_post_audit.post_id')->limit(5)->order_by('post_log_id desc')->get_all();

        	$this->data['log_admin'] = $this->Log_admin_activities_m->select('log_admin_activities.*, admins.display_name')->join('admins','admins.admin_id = log_admin_activities.admin_id')->limit(10)->order_by('log_id desc')->get_all();

        	$time_today = strtotime('today');
        	$this->data['total_post_publish'] = $this->post_m->num(array('post_status'=>1));
        	$this->data['today_post_publish'] = $this->post_m->where('post_time_start > ',$time_today)->num(array('post_status'=>1));

        	$this->data['total_post_deploy_waiting'] = $this->post_m->num(array('post_status'=>3));
        	$this->data['today_post_deploy_waiting'] = $this->post_m->num(array('post_status'=>3));

        	$this->data['total_post_approval_waiting'] = $this->post_m->num(array('post_status'=>2));
        	$this->data['today_post_approval_waiting'] = $this->post_m->num(array('post_status'=>2));

        	$this->data['total_post_down'] = $this->post_m->num(array('post_status'=>array(4,5,6)));
        	$this->data['today_post_down'] = $this->post_m->num(array('post_status'=>array(4,5,6)));

            $this->theme_data["main"] = "admin/dashboard";
            $this->template->viewAdmin($this->theme_data, $this->data);
        } else {
			$this->theme_data["left"] = null;
            $this->theme_data["main"] = "admin/login";
            $this->template->viewAdmin($this->theme_data, $this->data);
        }
    }
	
	function nopermission()
	{
		$data = array();
		$this->theme_data["main"] = "include/no-permission";
        $this->template->viewAdmin($this->theme_data, $data);
	}
	
	function page404()
	{
		$this->output->set_status_header('404');
		set_status_header(404);
		$this->data['noindex'] = TRUE;
		$this->theme_data["main"] = "include/page404";
		$this->template->viewAdmin($this->theme_data, $this->data);
	}
	
	function upload_process(){
		if (isset($_SERVER['HTTP_ORIGIN'])) {
			header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
			header('Access-Control-Allow-Credentials: true');
			header('Access-Control-Max-Age: 86400');    // cache for 1 day
		}
		
		if (isset($_REQUEST['progresskey'])) 
		$status = apc_fetch('upload_'.$_REQUEST['progresskey']);
		else 
		exit(json_encode(array('success' => false)));
		
		$pct = 0;
		$size = 0;
		if (is_array($status)) {
			if (array_key_exists('total', $status) && array_key_exists('current', $status)) {
				if ($status['total'] > 0) {
					$pct = round( ( $status['current'] / $status['total']) * 100 );
					$size = round($status['total'] / 1024);
				}
			}
		}
		
		echo json_encode(array('success' => true, 'pct' => $pct, 'size' => $size));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/Welcome.php */